# -*- coding: utf-8 -*-
# @Time    : 2019/10/25 11:07
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : thread_semaphore.py

# Semaphore 是用于控制进入数量的锁
# 文件、读，写，写一般是只允许一个线程写，读可以允许有多个

# 模拟爬虫
import threading
import time


class HtmlSpider(threading.Thread):
    def __init__(self, url, sem):
        super(HtmlSpider, self).__init__()
        self.url = url
        self.sem = sem

    def run(self):
        time.sleep(2)
        print("got html text success")
        self.sem.release()


class UrlProducer(threading.Thread):
    def __init__(self, sem):
        super().__init__()
        self.sem = sem

    def run(self):
        for i in range(20):
            self.sem.acquire()
            # sem对象的传入大于0的值时，执行一次数值减1，减至0变为阻塞
            # 必须要在调用的线程中release
            html_thread = HtmlSpider(f"www.baidu.com/{i}", self.sem)
            html_thread.start()


if __name__ == '__main__':
    sem = threading.Semaphore(3)
    url_producer = UrlProducer(sem)
    url_producer.start()